<?php
/**
 * @file r.tag.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-07 02:16:25 +0000 (Fri, 07 Jun 2013) $
 *
 * $Id: r.tag.php 71 2013-06-07 02:16:25Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/rss/r.tag.php $
 */

global $wcDatabaseAccess;

$db = WCConnection::Instance();
$dbprefix = $wcDatabaseAccess['prefix'];

$auxGroup = $wcGroupForbidden != false ? $wcGroupForbidden : $wcGroup;
$auxAction = $wcActionForbidden != false ? $wcActionForbidden : $wcAction;

$allowed = false;

if(defined('__RSS__') && $auxGroup && $auxGroup->isPublic() && $wcTag) {
	$allowed = true;
} elseif(defined('__RSS__') && $wcTag && isset($_REQUEST["uhash"])) {
	$stmt = null;
	$stmtId = str_replace($wcROOTDIR, "", __FILE__)."[uhash]";
	if($db->has($stmtId)) {
		$stmt = $db->get($stmtId);
	} else {
		$query = "select  usr_id\n";
		$query .= "from    {$dbprefix}users\n";
		$query .= "where   usr_hash_rss = :uhash\n";
		$stmt = $db->prepare($stmtId, $query);
	}

	$params = array(":uhash" => $_REQUEST["uhash"]);

	if($stmt->execute($params) && $stmt->rowCount() > 0) {
		$row = $stmt->fetch();
		$user = $wcUsersHolder->item($row["usr_id"]);
		if($user && $user->ok()) {
			$profile = new WCProfile();
			$profile->load($user);
			$allowed = ($profile->grantedGroup($auxGroup) && $profile->allowedTo(WC_PERM_TAG_VIEW));
		}
	}
}

if($allowed) {
	if($_SERVER["REQUEST_METHOD"] == "GET") {
		$url = "http://{$_SERVER["SERVER_NAME"]}:{$_SERVER["SERVER_PORT"]}{$wcROOTURI}";
		$index = "{$url}/index.php?group={$auxGroup->id()}";

		$wcRSS->title = "Tag: '{$wcTag->name}'";
		$wcRSS->description = "These are the last comics added to tag '{$wcTag->name}'.";
		$wcRSS->link = "{$index}&action=".WC_ACTION_TAG_SHOW."&tag={$wcTag->id()}";
		//$wcRSS->lastBuildDate = time();
		//$wcRSS->pubDate = time();
		//$wcRSS->ttl = "";

		$stmt = null;
		$stmtId = str_replace($wcROOTDIR, "", __FILE__)."[comics]";
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "select  unix_timestamp(mem_added) as mem_added,\n";
			$query .= "        cmc_id,\n";
			$query .= "        cmc_name,\n";
			$query .= "        cmc_next_name\n";
			$query .= "from    {$dbprefix}tag_members\n";
			$query .= "                inner join {$dbprefix}comics\n";
			$query .= "                        on (mem_comic = cmc_id)\n";
			$query .= "where   mem_group = :group\n";
			$query .= " and    mem_tag   = :tag\n";
			$query .= "order by mem_added desc\n";
			$stmt = $db->prepare($stmtId, $query);
		}

		$params = array(
			":tag" => $wcTag->id(),
			":group" => $auxGroup->id()
		);

		if($stmt->execute($params)) {
			foreach($stmt->fetchAll() as $row) {
				$comic = $wcComicsHolder->item($row["cmc_id"], $auxGroup);
				if($comic && $comic->ok() && $comic->hasPreview()) {
					$item = new WCRssItem();
					$name = $row["cmc_next_name"] ? $row["cmc_next_name"] : $row["cmc_name"];
					$item->title = $name;

					$html = "<h3>Comic '{$name}'</h3>";
					$html = "<img src=\"{$url}/{$comic->previewUri()}\" title=\"preview\"/><br/>";

					$item->description = $html;
					$item->link = "{$index}&action=".WC_ACTION_COMIC_SHOW."&comic={$row["cmc_id"]}";
					//$item->guid = "";
					$item->pubDate = $row["mem_added"];

					$wcRSS->addItem($item);
				}
			}
		}
	}
}
?>